<?php

function Success($message = 'ok', $data = [], $code = 0, $statusCode = 200)
{
    $res = [
        'code' => $code,
        'message' => $message,
        'result' => $data,
    ];
    return response()->json($res, $statusCode);
}

function Error($message = 'error', $code = -1, $data = [], $statusCode = 200)
{
    $res = [
        'code' => $code,
        'message' => $message,
        'result' => $data,
    ];
    return response()->json($res, $statusCode);
}

function uploadSuccess($message = 'ok', $url = [], $data = [], $code = 0, $statusCode = 200)
{
    $res = [
        'code' => $code,
        'message' => $message,
        'url' => $url,
        'result' => $data
    ];
    return response()->json($res, $statusCode);
}

function translateCountryToDomainId($country)
{
    $arr = ['us' => 1, 'uk' => 2, 'de' => 3, 'fr' => 4, 'jp' => 5, 'ca' => 6, 'it' => 8, 'es' => 9, 'in' => 10, 'mx' => 11];
    if ($country === '') {
        return $arr;
    }
    return element($country, $arr, '');
}

function element($item, $array, $default = FALSE)
{
    if (!isset($array[$item]) or $array[$item] == "") {
        return $default;
    }

    return $array[$item];
}

/**
 * 生成用户安全密钥
 * @param int $length
 * @return string
 */
function GenerateSafeKey($length = 16)
{
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $safe_key = '';
    for ($i = 0; $i < $length; $i++) {
        $safe_key .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $safe_key;
}

/**
 * 获取月份区间内所有月份
 * @param $startMonth
 * @param $endMonth
 * @param null $startYear
 * @param null $endYear
 * @return array
 */
function showMonthRange($start, $end)
{
    $end = date('Y-m', strtotime($end)); // 转换为月
    $range = [];
    $i = 0;
    do {
        $month = date('Y-m', strtotime($start . ' + ' . $i . ' month'));
        $range[] = $month;
        $i++;
    } while ($month < $end);

    return $range;
}

function get_last_two_month($specifiedMonth, $showCurrent = true)
{
    // 创建DateTime对象
    $date = new DateTime($specifiedMonth . "-01"); // 假设我们总是处理月份的第一天

    // 获取上个月
    $lastMonth = clone $date; // 克隆当前DateTime对象以避免修改原始对象
    $lastMonth->modify('-1 month');
    $lastMonthStr = $lastMonth->format('Y-m');

    // 获取上上个月
    $lastLastMonth = clone $date; // 再次克隆
    $lastLastMonth->modify('-2 month');
    $lastLastMonthStr = $lastLastMonth->format('Y-m');

    // 输出结果
    if ($showCurrent){
        return [$specifiedMonth, $lastMonthStr, $lastLastMonthStr];
    }else{
        return [$lastMonthStr, $lastLastMonthStr];
    }
}

function get_limit_type($type = ''){
    $asin_limit_type = [0 => '不在售', 1 => '店铺无', 2 => '侵权了', 3 => '被投诉了', 4 => '其他原因'];

    if ($type === '') {
        return '';
    }
    return element($type, $asin_limit_type, '');
}
